package snippets;
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class StartBrowser {

	/**
	 * @param args
	 * @throws URISyntaxException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, URISyntaxException {
		Desktop.getDesktop().browse(new URI("http://10.8.1.17"));
		Login l = new Login("admin", "adminpasswd");
		l.start();
	}
	static class Login extends Thread {

		private String user;
		private String passwd;

		public Login(String user, String passwd) {
			this.user = user;
			this.passwd = passwd;
		}
		@Override
		public void run() {
			try {
				Robot robot = new Robot();
				robot.delay(999);
				keyType(robot, user);
				keyType(robot, KeyEvent.VK_TAB);
				robot.delay(50);
				keyType(robot, passwd);
				keyType(robot, KeyEvent.VK_TAB);
				robot.delay(50);
				keyType(robot, KeyEvent.VK_ENTER);
				
			} catch (AWTException e) {
				e.printStackTrace();
			}
		}
		private void keyType(Robot robot, String s) {
			char[] tab = s.toCharArray();
			for (char ch : tab) {
				if (Character.isLowerCase(ch)) {
					keyType(robot, Character.toUpperCase(ch));
				} else if (Character.isUpperCase(ch)) {
					keyType(robot, KeyEvent.VK_SHIFT, ch);
				}
				//keyType(robot, ch);
			}
		}
		private void keyType(Robot robot, int... code) {
			for (int i=0 ; i<code.length ; i++)
				robot.keyPress(code[i]);
			for (int i=code.length-1 ; i>=0 ; i--)
				robot.keyRelease(code[i]);
		}
	}

}
